package bubbleindex;

import java.awt.EventQueue;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 *
 * @author ttrott
 */
public class BubbleIndex {
   
    public static String CategoryName = "";
    public static String SelectionName = "";
    public static float Omega;
    public static float M;
    public static float T_crit;
    public static List<Integer> windowLengths;
    public static List<String> DailyPriceDataList;
    public static List<String> DailyPriceDateList;
    public static String PreviousFilePath = "";
    public static String FilePath = "";
    public static String SavePath = "";
    public static float[] DailyPriceValues;
    public static Integer[] BacktestDayLengths;
    public static int ThreadNumber;
    
    /**
     * RunBubbleIndex creates the commands to read the daily price file, 
     * store the values in memory, obtains the number of Bubble Indices to create, 
     * obtains the backtest length window for each Bubble Index, sends the command 
     * to calculate all the The Bubble Indices, and finally outputs a string to 
     * the Java Swing TextArea
     * @throws java.util.concurrent.ExecutionException
     * @throws java.io.IOException
     * @throws java.lang.InterruptedException
     */
    public static void RunBubbleIndex() throws ExecutionException, IOException, InterruptedException {
        DailyPriceDataList = new ArrayList<>();
        DailyPriceDateList = new ArrayList<>();
        windowLengths = new ArrayList<>();
        setFilePaths();
        displayFilePaths();

        InputData.ReadValues(FilePath, DailyPriceDateList, DailyPriceDataList, false, false);

        final int DAILY_DATA_SIZE = DailyPriceDataList.size();

        DailyPriceValues = new float[DAILY_DATA_SIZE];
        InputData.ConvertPrices(DailyPriceValues, DAILY_DATA_SIZE);

        final int NUMBER_OF_INDICES = InputData.GetDayLengths();
        BacktestDayLengths = new Integer[NUMBER_OF_INDICES];
        BacktestDayLengths = windowLengths.toArray(BacktestDayLengths);

        setOmega_M_T();
        setThreads();
        
        BuildIndex.Create(DAILY_DATA_SIZE, NUMBER_OF_INDICES);

        displayOutput("Done", true);
    }
    
        /**
     * setFilePaths
     */
    private static void setFilePaths() {

        FilePath = Indices.userDir + "ProgramData" + 
                Indices.filePathSymbol + CategoryName + Indices.filePathSymbol +
                SelectionName + Indices.filePathSymbol + SelectionName + 
                "dailydata.csv";

        SavePath = Indices.userDir + "ProgramData" + Indices.filePathSymbol + 
                CategoryName + Indices.filePathSymbol + SelectionName + Indices.filePathSymbol;
    }

    private static void displayFilePaths() { 
        displayOutput("Output File Path: " + FilePath, false);
        displayOutput("Output Save Path: " + SavePath, false);
    }

    private static void setOmega_M_T() {
        Omega = Float.parseFloat(GUI.OmegaTextField.getText().trim());
        M = Float.parseFloat(GUI.MTextField.getText().trim());
        T_crit = Float.parseFloat(GUI.TCriticalField.getText().trim());
    }

    public static void setThreads() {
        ThreadNumber = Integer.parseInt(GUI.ThreadNumber.getText().trim());
    }
    
    public static void displayOutput(String displayText, boolean resetTextArea) {
        final String outputString = displayText;
        final boolean reset = resetTextArea;
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() { 
                if (reset) {
                    GUI.OutputText.setText("");
                }
                GUI.OutputText.append(outputString + "\n");
            }
        });
    }
}
